<?php

class UpyunAction extends AdminbaseAction{

	public $bucketname;
	public $username;
	public $password;
	public $url;
	public $upyun;
	public $domain;

	public function _initialize(){

		parent::_initialize();

		require_once 'Class/Upyun/upyun.class.php';

		$this->bucketname = '';
		$this->username = '';
		$this->password = '';
		$this->url = 'http://'.$this->bucketname.'.b0.upaiyun.com';
		$this->upyun = new UpYun($this->bucketname,$this->username,$this->password);

		$this->domain = '';

		// $this->upyun = $upyun;

	}

	/**
	 * 首页管理文件
	 * @return [type] [description]
	 */
	public function index(){
        $all_size = M('cloudfile')->where("cloud_type = 'upyun'")->sum('size');

        $this->all_size = $all_size;

		$list = M('cloudfile')->where("cloud_type = 'upyun'")->order('create_time DESC')->select();

		$this->assign('list',$list);

		// print_r($list);

		$this->display();
	}
	
	/**
	 * ueditor上传文件
	 * 存储到数据库
	 * @return [type] [description]
	 */
	public function ueditorUp(){
	    //获取存储目录
	    if ( isset( $_GET[ 'fetch' ] ) ) {
	        header( 'Content-Type: text/javascript' );
	        echo 'updateSavePath('. json_encode('/') .');';
	        return;
	    }

    	$file = $_FILES['upfile'];
    	$file_name = $file['name'];
    	$tmp = $file['tmp_name'];
    	$size = $file['size'];
    	// 以网站名称存储
    	$path = $this->domain;

    	$save_path = '/'.$this->domain.'/img/'.date("Ymd").'/'
    		.time().rand(0,10).rand(0,1000).
    		$this->getFileExt($file_name);

    	// echo $save_path;exit;
    	// 
    	$fp = fopen($tmp, 'r');
    	$res = $this->upyun->writeFile($save_path,$fp,true);
    	fclose($fp);

    	if(! empty($res) ){
    		$json = array(
    			'url' => $this->url.$save_path,
    			'title' => $file_name,
    			'original' => $file_name,
    			'state' => 'SUCCESS',
    		);

    		$data = array(
    			'user_id' => 0,
    			'term_id' => 0,
    			'object_id' => 0,
    			'cloud_type' => 'upyun',
    			'key' => $save_path,
    			'file_name' => $file_name,
    			'file_type' => $this->getFileExt($file_name),
    			'url' => $this->url.$save_path,
    			'size' => $size,
    			'create_time' => time()
    		);

    		$fid = M('cloudfile')->add($data);

    		if($fid){
    			echo json_encode($json);
    		}else{
    			echo json_encode(array('state'=>'写入数据库错误'));
    		}
    		
    	}else{
    		echo json_encode(array('state'=>'上传错误'));
    	}

	}

    public function swfUp($file){
        // print_r($file);
        // exit;
        $file_name = $file['name'];
        $tmp = $file['tmp_name'];
        $size = $file['size'];
        $path = $this->domain;

        $save_path = '/'.$this->domain.'/img/'.date("Ymd").'/'
            .time().rand(0,10).rand(0,1000).
            $this->getFileExt($file_name);

        $fp = fopen($tmp, 'r');
        $res = $this->upyun->writeFile($save_path,$fp,true);
        fclose($fp);

        $data = array(
            'user_id' => 0,
            'term_id' => 0,
            'object_id' => 0,
            'cloud_type' => 'upyun',
            'key' => $save_path,
            'file_name' => $file_name,
            'file_type' => $this->getFileExt($file_name),
            'url' => $this->url.$save_path,
            'size' => $size,
            'create_time' => time()
        );
        $fid = M('cloudfile')->add($data);

        echo "1," .$this->url.$save_path.",".'1,'.$file_name;
    }

    /**
     * 获取文件扩展名
     * @return string
     */
    private function getFileExt($name)
    {
        return strtolower( strrchr( $name , '.' ) );
    }

    /**
     * 在线管理
     * 获取文件地址
     * 可以使用文件处理
     * @return [type] [description]
     */
    public function manager(){
    	$list = M('cloudfile')->order('id desc')->select();
    	foreach ($list as $key => $value) {
    		$files[] = $value['url'];
            // .'_sw80';
    	}
        $str = "";
        foreach ( $files as $file ) {
            $str .= $file . "ue_separate_ue";
        }
        echo $str;
    }

    /**
     * 删除文件
     * 需要注意超时
     * @return [type] [description]
     */
    public function deleteFile(){

    	if($_GET['id']){
	   		$id = $_GET['id'];
	    	$path = M('cloudfile')->where('id='.$id)->getField('key');

	    	$did = M('cloudfile')->where('id='.$id)->delete();
	    	$res = $this->upyun->deleteFile($path);
	    	
	    	if(empty($res) && $did){
	    		$this->success('删除成功');
	    	}else{
	    		$this->error('删除失败');
	    	}
    	}

    	if($_POST['ids']){
    		foreach ($_POST['ids'] as $key => $value) {
    			$path = M('cloudfile')->where('id='.$value)->getField('key');
    			$res = $this->upyun->deleteFile($path);
                $des = M('cloudfile')->where('id='.$value)->delete();
    		}
	    	if(empty($res) && $des){
	    		$this->success('删除成功');
	    	}else{
	    		$this->error('删除失败');
	    	}
    	}
    }
}